import logger from '../core/logger';
import { clientBase } from './core/clientBase';
import { mangaInfo } from '../models/manga/mangaInfo';
import { mangaItem } from '../models/manga/mangaItem';
import { mangaType } from '../models/manga/mangaType';
import { partManga } from '../models/manga/partManga';
import { searchManga } from '../models/manga/searchManga';
import { recommendInfo } from '../models/manga/recommendInfo';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class mangaClient extends clientBase {
  getTypeList(page: number, styleId: number = -1, areaId: number = -1, isFree: number = -1, order: number = 3, isFinish: number = -1): Promise<partManga[] | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`style_id=${styleId}&area_id=${areaId}&is_finish=${isFinish}&order=${order}&is_free=${isFree}&page_num=${page}&page_size=15`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/ClassPage', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: Array<partManga> = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  getInfo(id: number): Promise<mangaInfo | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`comic_id=${id}`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/ComicDetail', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: mangaInfo = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  getRecommend(page: number): Promise<recommendInfo | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`page_num=${page}&omit_cards=1&drag=0&new_fall_into_trap=0`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/HomeRecommend', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: recommendInfo = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  getRank(): Promise<mangaItem[] | string> {
    return new Promise((resolve) => {
      try {
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/HomeHot')
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: Array<mangaItem> = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  getSearch(keywords: string, page: number): Promise<searchManga | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`key_word=${encodeURIComponent(keywords)}&page_num=${page}&page_size=20`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/Search', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: searchManga = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  getType(): Promise<mangaType | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam();
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/comic.v1.Comic/AllLabel', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: mangaType = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }
}
export default new mangaClient();